package com.sikaryofficial.backend.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sikaryofficial.backend.domain.entity.ProductScanDetail;
import com.sikaryofficial.backend.domain.entity.ProductStatistics;
import com.sikaryofficial.backend.service.IProductScanDetailService;
import com.sikaryofficial.backend.service.IProductStatisticsService;
import com.sikaryofficial.backend.service.brand.cache.BrandTasteCacheService;
import com.sikaryofficial.backend.utils.ProductUtil;
import com.sikaryofficial.common.core.constant.CacheBusinessConstants;
import com.sikaryofficial.common.redis.service.RedisService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.model.product.BrandTasteDTO;
import com.sikaryofficial.system.api.model.product.ProductStatisticsReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 产品统计管理
 * @date : 2023/10/30 15:39
 */
@Service
@Slf4j
@RefreshScope
public class ProductStaticsManager {
    @Autowired
    private RedisService redisService;
    @Autowired
    private BrandTasteCacheService brandCacheService;
    @Autowired
    private IProductStatisticsService productStatisticsService;
    @Autowired
    private IProductScanDetailService productScanDetailService;
    public boolean updateStatisticsAndCache(String param) {

        Integer productShortId = Integer.parseInt(param.split(ProductUtil.PARAM_SPLIT_KEY)[0]);
        BrandTasteDTO brandTasteDTO = brandCacheService.getSignBrandTasteByShortId(productShortId);
        Long brandTasteId = brandTasteDTO.getBrandTasteId();

        // 2、记录扫码获取100积分详情数据
        saveDetail(brandTasteId);

        // 3、统计数据处理
        saveStatistics(brandTasteId);

        // 4、缓存处理
        processCacheData(brandTasteDTO, brandTasteId);
        return true;
    }

    private void processCacheData(BrandTasteDTO brandTasteDTO, Long brandTasteId) {
        // 3、缓存处理
        ProductStatistics productStatistics = productStatisticsService.getOne(new LambdaQueryWrapper<ProductStatistics>().eq(ProductStatistics::getBrandTasteId, brandTasteId).eq(ProductStatistics::getDeletedVersion, 0L).last(" limit 1 "));
        String cacheKey = MessageFormat.format(CacheBusinessConstants.PRODUCT_QRCODE_HUNDRED_CACHE, brandTasteDTO.getBrandTasteId().toString());
        Long hundredNum = productStatistics.getScanCount();
        redisService.setCacheObject(cacheKey, Objects.isNull(hundredNum) ? 1 : hundredNum);
    }

    private void saveStatistics(Long brandTasteId) {
        // 2、统计数据处理
        ProductStatistics productStatistics = new ProductStatistics();
        productStatistics.setProductStatisticsId(IdWorker.getId());
        productStatistics.setBrandTasteId(brandTasteId);
        productStatistics.setScanCount(1L);
        productStatistics.setCreatedBy(SecurityUtils.getUserId());
        productStatistics.setUpdatedBy(SecurityUtils.getUserId());
        productStatisticsService.insertStatisticsData(productStatistics);
    }

    private void saveDetail(Long brandTasteId) {
        // 1、记录扫码获取100积分详情数据
        productScanDetailService.save(ProductScanDetail.builder().brandTasteId(brandTasteId).createdBy(SecurityUtils.getUserId()).createdTime(new Date()).deletedVersion(0L).build());
    }

    @Async("asyncTaskExecutor")
    public Boolean saveData(ProductStatisticsReq req) {
        // 2、统计数据处理
        ProductStatistics productStatistics = new ProductStatistics();
        productStatistics.setProductStatisticsId(IdWorker.getId());
        productStatistics.setBrandTasteId(req.getBrandTasteId());
        productStatistics.setNormalScanCount(Objects.isNull(req.getNormalScanCount()) ? 0L : req.getNormalScanCount());
        productStatistics.setCreatedBy(SecurityUtils.getUserId());
        productStatistics.setUpdatedBy(SecurityUtils.getUserId());
        return productStatisticsService.insertNormalStatisticsData(productStatistics) > 0;
    }
}
